package com.ev.apis.controller.hr;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

import com.ev.framework.annotation.EvApiByToken;
import com.ev.hr.enums.EmployeeStateDict;
import com.ev.hr.vo.EmployeeInfoSaveParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.ev.hr.domain.EmployeeInfoDO;
import com.ev.hr.vo.EmployeeInfoPageParam;
import com.ev.hr.service.EmployeeInfoService;
import com.ev.framework.utils.R;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件名称： com.ev.hr.EmployeeInfoApiController.java</br>
 * 初始作者： ev-monitor</br>
 * 创建日期： 2020-05-08 09:47:13</br>
 * 功能说明： 员工信息表 <br/>
 * =================================================<br/>
 * 修改记录：<br/>
 * 修改作者        日期       修改内容<br/>
 * ================================================<br/>
 * Copyright (c) 2020-2021 .All rights reserved.<br/>
 */

@Api(value = "/",tags = "员工信息表API")
@RestController
public class EmployeeInfoApiController {

	/**
	 * 员工信息表 服务类
	 */
	@Autowired
	private EmployeeInfoService employeeInfoService;

	/**
	 * 方法描述: [根据条件分页查询员工信息表.]</br>
	 * 初始作者: ev-monitor<br/>
	 * 创建日期: 2020-05-08 09:47:13<br/>
	 * 开始版本: 1.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 *
	 * @param pageParam 分页参数
	 * @return R 响应对象
	 */
	@EvApiByToken(value = "/apis/employeeInfo/pageList", method = RequestMethod.POST, apiTitle = "根据条件分页查询员工信息表")
	@ApiOperation(value = "根据条件分页查询员工信息表", produces = MediaType.APPLICATION_JSON_VALUE)
	public R pageList(@Valid @RequestBody @ApiParam("员工信息表分页列表参数对象") EmployeeInfoPageParam pageParam) {
		return R.ok(this.employeeInfoService.pageList(pageParam));
	}

	/**
	 * 方法描述: [根据id获取员工信息表.]</br>
	 * 初始作者: ev-monitor<br/>
	 * 创建日期: 2020-05-08 09:47:13<br/>
	 * 开始版本: 1.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 *
	 * @param id 主键
	 * @return R 响应对象
	 */
	@EvApiByToken(value = "/apis/employeeInfo/detail/{id}", method = RequestMethod.GET, apiTitle = "根据id获取员工信息表")
	@ApiOperation("根据id获取员工信息表")
	public R detail(@PathVariable("id") @ApiParam("id") Long id) {
		return R.ok(this.employeeInfoService.getById(id));
	}

	/**
	 * 方法描述: [批量删除]</br>
	 * 初始作者: ev-monitor<br/>
	 * 创建日期: 2020-05-08 09:47:13<br/>
	 * 开始版本: 1.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 *
	 * @param ids ID组
	 * @return R 响应对象
	 */
	@EvApiByToken(value = "/apis/employeeInfo/batchRemove", method = RequestMethod.POST, apiTitle = "批量删除")
	@ApiOperation(value = "批量删除", notes = "批量删除", produces = MediaType.APPLICATION_JSON_VALUE)
	@Transactional(rollbackFor = Exception.class)
	public R batchRemove(@RequestBody @ApiParam("批量删除") List<Long> ids){
		Long[] idsToLong = ids.toArray(new Long[0]);
		return this.employeeInfoService.batchRemove(idsToLong)>0?R.ok():R.error();
	}

	/**
	 * 方法描述: [保存员工信息表.]</br>
	 * 初始作者: ev-monitor<br/>
	 * 创建日期: 2020-05-08 09:47:13<br/>
	 * 开始版本: 1.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 *
	 * @param saveParam 员工信息表参数
	 * @return R 响应对象
	 */
	@Transactional(rollbackFor = Exception.class)
	@EvApiByToken(value = "/apis/employeeInfo/save", method = RequestMethod.POST, apiTitle = "保存员工信息表")
	@ApiOperation("保存员工信息表")
	public R save(@Valid @RequestBody @ApiParam("保存员工信息表对象") EmployeeInfoSaveParam saveParam){
		return this.employeeInfoService.saveAndVerify(saveParam);
	}

	/**
	 * 方法描述: [保存员工扩展信息.]</br>
	 * 初始作者: ev-monitor<br/>
	 * 创建日期: 2020-05-19 09:47:13<br/>
	 * 开始版本: 1.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 *
	 * @param saveParam 员工信息表参数
	 * @return R 响应对象
	 */
	@Transactional(rollbackFor = Exception.class)
	@EvApiByToken(value = "/apis/employeeInfo/saveExtension", method = RequestMethod.POST, apiTitle = "保存员工扩展信息")
	@ApiOperation("保存员工扩展信息")
	public R saveExtension(@Valid @RequestBody @ApiParam("保存员工扩展信息") EmployeeInfoDO saveParam){
		this.employeeInfoService.update(saveParam);
		return R.ok();
	}

	/**
	 * 方法描述: [批量转正.]</br>
	 * 初始作者: ev-monitor<br/>
	 * 创建日期: 2020-05-19 09:47:13<br/>
	 * 开始版本: 1.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 *
	 * @param ids 员工信息表参数
	 * @return R 响应对象
	 */
	@Transactional(rollbackFor = Exception.class)
	@EvApiByToken(value = "/apis/employeeInfo/batchFull", method = RequestMethod.POST, apiTitle = "批量转正")
	@ApiOperation("批量转正")
	public R batchFull(@RequestBody @ApiParam("批量转正ids") List<Long> ids){
		Date now = new Date();
		for(Long id : ids){
			EmployeeInfoDO employeeInfoDO = employeeInfoService.get(id);
			employeeInfoDO.setEmploymentState(EmployeeStateDict.WORK.getId());
			employeeInfoDO.setFormalDate(now);
			this.employeeInfoService.update(employeeInfoDO);
		}

		return R.ok();
	}

	/**
	 * 方法描述: [批量离职.]</br>
	 * 初始作者: ev-monitor<br/>
	 * 创建日期: 2020-05-19 09:47:13<br/>
	 * 开始版本: 1.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 *
	 * @param ids 员工信息表参数
	 * @return R 响应对象
	 */
	@Transactional(rollbackFor = Exception.class)
	@EvApiByToken(value = "/apis/employeeInfo/batchLeave", method = RequestMethod.POST, apiTitle = "批量离职")
	@ApiOperation("批量离职")
	public R batchLeave(@RequestBody @ApiParam("批量离职ids") List<Long> ids){
		Date now = new Date();
		for(Long id : ids){
			EmployeeInfoDO employeeInfoDO = employeeInfoService.get(id);
			employeeInfoDO.setEmploymentState(EmployeeStateDict.RESIGNATION.getId());
			employeeInfoDO.setQuitDate(now);
			this.employeeInfoService.update(employeeInfoDO);
		}
		return R.ok();
	}

	/**
	 * 方法描述: [批量激活.]</br>
	 * 初始作者: ev-monitor<br/>
	 * 创建日期: 2020-05-19 09:47:13<br/>
	 * 开始版本: 1.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 *
	 * @param ids 员工信息表参数
	 * @return R 响应对象
	 */
	@Transactional(rollbackFor = Exception.class)
	@EvApiByToken(value = "/apis/employeeInfo/batchActive", method = RequestMethod.POST, apiTitle = "批量激活")
	@ApiOperation("批量激活")
	public R batchActive(@RequestBody @ApiParam("批量激活ids") List<Long> ids){
		for(Long id : ids){
			EmployeeInfoDO employeeInfoDO = employeeInfoService.get(id);
			employeeInfoDO.setEmploymentState(EmployeeStateDict.WORK.getId());
			this.employeeInfoService.update(employeeInfoDO);
		}
		return R.ok();
	}



	/**
	 * 方法描述: [员工导入]</br>
	 * 初始作者:  xy </br>
	 * 创建日期: 2020/6/5 14:54<br/>
	 * 开始版本: 1.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 * @param file
	 * @Return : com.ev.framework.utils.R
	 */
	@Transactional(rollbackFor = Exception.class)
	@EvApiByToken(value = "/apis/employeeInfo/import", method = RequestMethod.POST, apiTitle = "员工导入")
	@ApiOperation("员工导入")
	public R importEmployees(@ApiParam(value = "文件信息", required = true) @RequestParam("file") MultipartFile file){
		return employeeInfoService.importRecord(file);
	}
	
}
